package com.ksu.manager.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.ksu.manager.model.User;
import com.ksu.manager.service.IUserService;

/**
 * 用户controller
 * 
 * @author lambert
 * 
 */

@Controller
@RequestMapping("/user")
public class UserController {

	@SuppressWarnings("unused")
	private static Logger log = Logger.getLogger(UserController.class);
	private IUserService userService;



	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response,User user) {
		ModelAndView mv = new ModelAndView("/user/list");
		try {
			List<User> userList = userService.queryList(user);
			mv.addObject("userList", userList);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return mv;
	}
	
	@RequestMapping(value = "/add")
	public ModelAndView add(HttpServletRequest request, HttpServletResponse response,User user){
		if(user!=null){
			userService.add(user);
		}
		return query(request,response,null);
	}
	
	@RequestMapping(value = "/del")
	public ModelAndView del(HttpServletRequest request, HttpServletResponse response,User user){
		if(user!=null){
			userService.del(user);
		}
		return query(request,response,null);
	}
	
	@RequestMapping(value = "/mod")
	public ModelAndView mod(HttpServletRequest request, HttpServletResponse response,User user){
		if(user!=null){
			userService.mod(user);
		}
		ModelAndView mv = new ModelAndView("/login/result");
		mv.addObject("result","refreshParent");
		return mv;
	}
	
	@RequestMapping(value = "/getUser")
	public ModelAndView getUserById(HttpServletRequest request, HttpServletResponse response,User user){
		ModelAndView mv = new ModelAndView("/user/mod");
		user = userService.queryUserById(user);
		mv.addObject(user);
		return mv; 
	}

}
